// pages/evaluate/evaluate.js
import qiniuUploader from "../../utils/qiniuUploader"

let api = require("../../api/api.js")
let myUrl = require("../../utils/url.js")

const app = getApp();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    current:0,
    attitude:true,
    time: true,
    efficiency: true,
    environment:true,
    professional:true,
    userStars: [
      "/pages/image/start_active.png",
      "/pages/image/start_active.png",
      "/pages/image/start_active.png",
      "/pages/image/start_active.png",
      "/pages/image/start_active.png",
    ],
    wjxScore:5,
    // textarea
    min: 5,//最少字数
    max: 300, //最多字数 (根据自己需求改变)
    pics:[],
    courseList:[], //订单的所有课程
    orderEvaluateList:[], //所有订单的评价
    userStarsMany:[], //多个星星数组
    orderInfo:{}, //订单信息
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options)
    let that = this
    let order_id = options.order_id
    api.orderDetial(order_id).then(res=>{
      console.log(res)
      that.setData({ orderInfo: res.data })
      that.setData({ courseList: res.data.course })
      that.setData({ orderEvaluateList: new Array(res.data.course.length) })
      let manyStart = []
        for(let i = 0; i < that.data.courseList.length; i ++){
          var course_id = "orderEvaluateList[" + i + "].course_id";
          that.setData({ [course_id]:that.data.courseList[i].course.id })
          var star_num = "orderEvaluateList["+ i +"].star_num";
          that.setData({ [star_num]:5 })
          var context = "orderEvaluateList["+ i +"].context"
          that.setData({ [context]:"" })
          var images = "orderEvaluateList["+ i +"].images"
          that.setData({ [images]:[] })
          manyStart.push(that.data.userStars)
        }
        that.setData({ userStarsMany: manyStart })


      wx.hideLoading()
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  //选取图片
  choose: function (e) {//这里是选取图片的方法
    var that = this;
    var index = e.currentTarget.dataset.index; // 当前是第几个订单
    var pics = that.data.orderEvaluateList[index].images
    console.log(that.data.orderEvaluateList)
    wx.chooseImage({
      count: 3 - pics.length, // 最多可以选择的图片张数，默认9
      sizeType: ['original', 'compressed'], // original 原图，compressed 压缩图，默认二者都有
      sourceType: ['album', 'camera'], // album 从相册选图，camera 使用相机，默认二者都有
      success: function (res) {
        console.log(res)
        var imgsrc = res.tempFilePaths[0];
        that.fileUpload(pics,index, imgsrc)
      },
      fail: function () {
        // fail
      },
      complete: function () {
        // complete
      }
    })
  },

  // 删除图片
  deleteImg: function (e) {
    console.log(this.data.orderEvaluateList)
    var image_index = e.currentTarget.dataset.imi;
    var order_index = e.currentTarget.dataset.oi
    var temp_img = this.data.orderEvaluateList[order_index].images
    temp_img.splice(image_index, 1);
    var orderImage = "orderEvaluateList[" + order_index + "].images"
    this.setData({
      [orderImage]: temp_img
    });
  },

  // 预览图片
  previewImg: function (e) {
    //获取当前图片的下标
    console.log(e)
    var image_index = e.currentTarget.dataset.imi;
    var order_index = e.currentTarget.dataset.oi
    //所有图片
    var pics = this.data.orderEvaluateList[order_index].images;
    wx.previewImage({
      //当前显示图片
      current: pics[image_index],
      //所有图片
      urls: pics
    })
  },

    // 星星点击事件
    starTap: function (e) {
      var that = this;
      var num = e.currentTarget.dataset.num; // 当前是第几颗星星
      var index = e.currentTarget.dataset.index; // 当前是第几个订单
      var tempUserStars = that.data.userStarsMany[index]; // 暂存星星数组
      let len = that.data.userStarsMany[index].length
      
      for (var i = 0; i < len; i++) {
        if (i <= num) { // 小于等于index的是满心
          tempUserStars[i] = "/pages/image/start_active.png";
          that.setData({
            wjxScore: i + 1,
          })
        } else { // 其他是空心
          tempUserStars[i] = "/pages/image/start.png"
        }
      }
      // 重新赋值就可以显示了
      var starsTmp = "userStarsMany["+ index +"]"
      var starsNumTmp = "orderEvaluateList["+ index +"].star_num"
      that.setData({
        [starsTmp]: tempUserStars,
      })
      that.setData({
        [starsNumTmp]: num + 1
      })
  
    },

  // 留言
  //字数限制
  inputs: function (e) {
    console.log(e)
    let index = e.currentTarget.dataset.index
    // 获取输入框的内容
    var value = e.detail.value;
    // 获取输入框内容的长度
    var len = parseInt(value.length);
    //最多字数限制
    if (len > this.data.max) return;
    // 获取输入的评论
    var content = "orderEvaluateList[" + index + "].context";
    this.setData({
      [content]:value
    })
    console.log(this.data)
  },


  //文件上传
  fileUpload:function(pics, index, path){
    var that = this
    var indexSuffix = path.lastIndexOf(".");
    var suffix = path.substr(indexSuffix+1);
    var qiniu_key = new Date().getTime()+"."+suffix
    console.log(path)
    //七牛云文件上传
    qiniuUploader.upload(path,
      (res)=>{
        console.log(res)
        let images = "orderEvaluateList["+index+"].images"
        pics.push(res.imageURL)
        that.setData({
          [images]: pics,
        });
        console.log(that.data.orderEvaluateList)
      },
      (error)=>{
        console.log(error)
      },
      {
        key: qiniu_key,
        region: 'SCN',
        uptokenURL: myUrl.mainUrl+"uploadtoken/?key="+qiniu_key,
        domain: app.globalData.qiniuDomain
      }
    )
  },

    //提交评价数据
  submitEvaluate:function(){
    var that = this
    console.log(that.data.orderEvaluateList)
    api.evaluateUpload(this.data.orderInfo.order_sn, this.data.orderEvaluateList).then(res=>{
      console.log(res)
      wx.hideLoading()
    })
  },


})